/*
 * @Author: xiangmin 
 * @File: 封装post和get请求
 * @Date: 2018-08-13 16:53:57 
 * @Last Modified by: xiangmin
 * @Last Modified time: 2018-09-17 20:34:12
 */
import Axios from 'axios';
import qs from 'qs';
import Cookies from 'js-cookie';
import {
    Message
} from 'element-ui';

Axios.defaults.withCredentials = true;

// 请求拦截器
Axios.interceptors.request.use(
    config => {
        const token = Cookies.get('accessToken');
        token && (config.headers.accessToken = token)
        return config;
    }
);

// 响应拦截器 给401等无权限做出响应 
Axios.interceptors.response.use( response => {
    return response;
},  error => {
    //Token失效
    if (error.response.status == 401) {
        //没有接口访问权限
    } else if (error.response.status == 403) {
    } else {
        failMessage();
    }
    return Promise.reject(error);
});

// Axios.defaults.headers.common['accessToken'] = '6f9ca014572342b7884ae3a0eb0b7446';
/**
 * post
 * @param  {String} url    [地址]
 * @param  {Object} params [参数]
 * @return {Object}        [Promise]
 */
export const get = (url, params) => {
    return new Promise((resolve, reject) => {
        Axios.get(url, {
            params: {
                ...params,
                _t: new Date().getTime()
            }
        }).then((res) => {
            if (res.status == 200) {
                if (res.data.code == 0 || res.status == 200) {
                    resolve(res.data)
                } else {
                    failMessage(res.data.message);
                }
            } else {
                failMessage()
                reject(res)
            }
        }).catch((mes) => {
            failMessage()
            reject(mes)
        })
    })
}
/**
 * post
 * @param  {String} url    [地址]
 * @param  {Object} params [参数]
 * @param  {String} type   [可不传，设定为form为formdata提交]
 * @return {Object}        [Promise]
 */
export const post = (url, params, type) => {
    if (type == 'form') {
        return new Promise((resolve, reject) => {
            Axios.post(url, qs.stringify(params)).then((res) => {
                if (res.status == 200) {
                    if (res.data.errorCode == 200) {
                        resolve(res.data)
                    } else {
                        failMessage(res.data.errorMessage)
                        reject(res)
                    }
                } else {
                    failMessage()
                    reject(res)
                }
            }).catch((mes) => {
                failMessage()
                reject(mes)
            })
        })
    } else {
        return new Promise((resolve, reject) => {
            let type = Object.prototype.toString.call(params);
            if (type == '[object Array]') {
                Axios.post(url, params).then((res) => {
                    if (res.status == 200) {
                        if (res.data.errorCode == 200) {
                            resolve(res.data)
                        } else {
                            failMessage(res.data.errorMessage)
                            reject(res)
                        }
                    } else {
                        failMessage()
                        reject(res)
                    }
                }).catch((mes) => {
                    failMessage()
                    reject(mes)
                })
            } else {
                Axios.post(url, {
                    ...params
                }).then((res) => {
                    if (res.status == 200) {
                        if (res.data.errorCode == 200) {
                            resolve(res.data)
                        } else {
                            failMessage(res.data.errorMessage);
                            reject(res)
                        }
                    } else {
                        failMessage()
                        reject(res)
                    }
                }).catch((mes) => {
                    failMessage()
                    reject(mes)
                })
            }
        })
    }
}

function failMessage(msg = '数据获取失败') {
    Message.error({
        message: msg,
        type: 'error'
    });
}
